package com.extension.company.view.home;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.extension.company.admin.R;
import com.extension.company.common.mvc.BaseTitleActivity;
import com.extension.company.view.adapter.GridImageAdapter;
import com.extension.company.common.widget.FullyGridLayoutManager;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.permissions.RxPermissions;
import com.luck.picture.lib.tools.PictureFileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class DemandReleaseActivity extends BaseTitleActivity {
	@BindView(R.id.demand_release_recycler)
	RecyclerView demandRecycleView;

	@BindView(R.id.btn_add)
	Button btn;


	private GridImageAdapter adapter;
	private List<LocalMedia> selectList = new ArrayList<>();

	public static void startActivity(Context context) {
		Intent intent = new Intent(context, DemandReleaseActivity.class);
		context.startActivity(intent);
	}

	@Override
	public int getMainLayout() {
		return R.layout.activity_demand_release;
	}


	@Override
	public void initView() {
		updateSuccessView();
		FullyGridLayoutManager manager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
		demandRecycleView.setLayoutManager(manager);
		adapter = new GridImageAdapter(this, onAddPicClickListener);
		adapter.setList(selectList);
		demandRecycleView.setAdapter(adapter);


		adapter.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(int position, View v) {
				if (selectList.size() > 0) {
					LocalMedia media = selectList.get(position);
					String pictureType = media.getPictureType();
					int mediaType = PictureMimeType.pictureToVideo(pictureType);
					switch (mediaType) {
						case 1:
							// 预览图片 可自定长按保存路径
							//PictureSelector.create(MainActivity.this).themeStyle(themeId).externalPicturePreview(position, "/custom_file", selectList);
							PictureSelector.create(DemandReleaseActivity.this).themeStyle(R.style.picture_Sina_style).openExternalPreview(position, selectList);
							break;

					}
				}
			}
		});


		RxPermissions permissions = new RxPermissions(this);
		permissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Observer<Boolean>() {
			@Override
			public void onSubscribe(Disposable d) {
			}

			@Override
			public void onNext(Boolean aBoolean) {
				if (aBoolean) {
					PictureFileUtils.deleteCacheDirFile(DemandReleaseActivity.this);
				} else {
					Toast.makeText(DemandReleaseActivity.this,
							getString(R.string.picture_jurisdiction), Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onError(Throwable e) {
			}

			@Override
			public void onComplete() {
			}
		});

	}

	@Override
	public void initData() {

	}

	@Override
	public void initListener() {

		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				MyDataHelperUtil myDataHelperUtil = new MyDataHelperUtil(DemandReleaseActivity.this);
				myDataHelperUtil.getWritableDatabase();

			}
		});

	}


	private GridImageAdapter.onAddPicClickListener onAddPicClickListener = new GridImageAdapter.onAddPicClickListener() {
		@Override
		public void onAddPicClick() {

			// 进入相册 以下是例子：不需要的api可以不写
			PictureSelector.create(DemandReleaseActivity.this)
					.openGallery(PictureMimeType.ofImage())// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
					.theme(R.style.picture_Sina_style)// 主题样式设置 具体参考 values/styles   用法：R.style.picture.white.style
					.maxSelectNum(4)// 最大图片选择数量
					.minSelectNum(1)// 最小选择数量
					.imageSpanCount(4)// 每行显示个数
					.selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选
					.previewImage(true)// 是否可预览图片
					.isCamera(true)// 是否显示拍照按钮
					.isZoomAnim(true)// 图片列表点击 缩放效果 默认true
					//.imageFormat(PictureMimeType.PNG)// 拍照保存图片格式后缀,默认jpeg
					//.setOutputCameraPath("/CustomPath")// 自定义拍照保存路径
					.enableCrop(false)// 是否裁剪
					.compress(true)// 是否压缩
					.synOrAsy(true)//同步true或异步false 压缩 默认同步
					.compressSavePath(getPath())//压缩图片保存地址
					//.sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
					.glideOverride(160, 160)// glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
					.withAspectRatio(1, 1)// 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
					.hideBottomControls(true)// 是否显示uCrop工具栏，默认不显示
					.isGif(false)// 是否显示gif图片
					.freeStyleCropEnabled(true)// 裁剪框是否可拖拽
					.circleDimmedLayer(false)// 是否圆形裁剪
					.showCropFrame(false)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false
					.showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false
					.openClickSound(false)// 是否开启点击声音
					.selectionMedia(selectList)// 是否传入已选图片
					.minimumCompressSize(100)// 小于100kb的图片不压缩
					.forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
		}


	};


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
				case PictureConfig.CHOOSE_REQUEST:
					// 图片选择结果回调
					selectList = PictureSelector.obtainMultipleResult(data);
					// 例如 LocalMedia 里面返回三种path
					// 1.media.getPath(); 为原图path
					// 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
					// 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
					// 如果裁剪并压缩了，已取压缩路径为准，因为是先裁剪后压缩的
					for (LocalMedia media : selectList) {
						Log.i("图片-----》", media.getPath());
					}
					adapter.setList(selectList);
					adapter.notifyDataSetChanged();
					break;
			}
		}
	}

	/**
	 * 自定义压缩存储地址
	 *
	 * @return
	 */
	private String getPath() {
		String path = Environment.getExternalStorageDirectory() + "/Luban/image/";
		File file = new File(path);
		if (file.mkdirs()) {
			return path;
		}
		return path;
	}


}
